package com.easou.sdk.service;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;

import com.easou.sdk.dao.PaymentOrderDao;
import com.easou.sdk.model.TradeInfo;
import com.easou.sdk.utils.Assert;
import com.easou.sdk.utils.json.Json;

public abstract class BasePartnerService implements PartnerService {

	protected PaymentOrderDao paymentOrderDao;


	@Override
	public String createOrder(String partnerId, String serverId, String partnerUserId, BigDecimal amount, String tradeName) {
		TradeInfo tradeInfo = createOrderInfo(partnerId, serverId, partnerUserId, amount, tradeName, "1000");
		return Json.toJson(tradeInfo);
	}

	@Override
	public String createOrder(String partnerId, String serverId, String partnerUserId, BigDecimal amount, String tradeName, String qn) {
		TradeInfo tradeInfo = createOrderInfo(partnerId, serverId, partnerUserId, amount, tradeName, qn);
		return Json.toJson(tradeInfo);
	}

	protected TradeInfo createOrderInfo(String partnerId, String serverId, String partnerUserId, BigDecimal amount, String tradeName) {
		return createOrderInfo(partnerId, serverId, partnerUserId, amount, tradeName, "1000");
	}

	protected TradeInfo createOrderInfo(String partnerId, String serverId, String partnerUserId, BigDecimal amount, String tradeName, String qn) {
		Assert.notEmtpy(partnerId, PARAM_ERROR, "合作商ID不能为空");
		Assert.notEmtpy(serverId, PARAM_ERROR, "服务器ID不能为空");
		Assert.notEmtpy(partnerUserId, PARAM_ERROR, "合作商用户ID不能为空");
		Assert.notEmtpy(amount, PARAM_ERROR, "订单金额不能为空");

		String gameId = "ldsg";

//		PaymentOrder paymentOrder = new PaymentOrder();
////		PaymentOrder lastPaymentOrder = this.paymentOrderDao.getLastOrder(gameId, partnerId);
////		int seq = 1;
////		if (lastPaymentOrder != null) {
////			seq = lastPaymentOrder.getSeq() + 1;
////		}
////		String orderId = PaymentOrderHelper.getOrderId(gameId, partnerId, seq);
//
//		Date now = new Date();
//
//		paymentOrder.setCreatedTime(now);
//		paymentOrder.setGameId(gameId);
//		paymentOrder.setOrderId(orderId);
//		paymentOrder.setPartnerId(partnerId);
//		paymentOrder.setPartnerUserId(partnerUserId);
//		paymentOrder.setSeq(seq);
//		paymentOrder.setAmount(amount);
//		paymentOrder.setFinishAmount(new BigDecimal(0));
//		paymentOrder.setServerId(serverId);
//		paymentOrder.setStatus(OrderStatus.STATUS_NEW);
//		paymentOrder.setUpdatedTime(now);
//
//		boolean success = true;
////		success = this.paymentOrderDao.add(paymentOrder);
//		if (!success) {
//			throw new ServiceException(ServiceReturnCode.FAILD, "订单创建失败");
//		}
//
//		TradeInfo tradeInfo = new TradeInfo();
//		tradeInfo.setExectInfo("");
//		tradeInfo.setPayerId(partnerUserId);
//		tradeInfo.setReqFee(String.valueOf(amount));
//		tradeInfo.setTradeDesc(tradeName);
//		tradeInfo.setTradeId(orderId);
//		tradeInfo.setTradeName(tradeName);
//		tradeInfo.setQn(qn);
//		tradeInfo.setPkgId(qn);
//		return tradeInfo;
		return null;
	}

	/**
	 * 检验登录信息
	 * @param token
	 * @param partnerId
	 * @param serverId
	 * @param timestamp
	 * @param sign
	 */
	protected void checkSign(String token, String partnerId, String serverId, long timestamp, String sign) {
//		String serverSign = EncryptUtil.getMD5((token + partnerId + serverId + timestamp + Config.ins().getSignKey()));
//		if (!serverSign.toLowerCase().equals(sign.toLowerCase())) {
//			throw new ServiceException(WebApiService.SIGN_CHECK_ERROR, "签名校验失败");
//		}
	}
}
